Exercises
Extract components
Let's practice creating components! In the exercises below, you'll be given a chunk of JSX, and your mission is to refactor the code so that it uses a component.
Building a CRM
Let's suppose we're building software.
We've written the markup to display the contact information for 3 contacts, but there's an awful lot of repetition involved. Let's create a new component, ContactCard
, and use that component for each of the 3 contacts.
Acceptance Criteria:
- A brand-new
ContactCard
component should be created and used for each of the 3 contact cards - Props should be created for any bits of information that vary from card to card.
Code Playground
Solution:
In this video, we mention that there are ways to dynamically change the rendered tag. This is known as “polymorphism”, and we learn all about it in Module 4.
Creating a “Button” component
Below, we have two <button>
elements. Create a Button
component, and use it to render both buttons.
Acceptance Criteria:
- A brand-new
Button
component should be created and used for both buttons - Each Button instance should be able to set its own text content, as well as choose a text/border color. It's up to you to decide how you want to structure this!
Code Playground
Solution: